package top.lbqaq.servlet;

import top.lbqaq.pojo.Supplier;
import top.lbqaq.service.SupplierService;
import top.lbqaq.util.Constants;
import top.lbqaq.util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author luoboQAQ
 * @Date 2021/11/30
 */
@WebServlet("/supplier")
public class SupplierServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        if ("queryAll".equals(method)) {
            this.queryAll(req, resp);
        } else if ("edit".equals(method)) {
            this.edit(req, resp);
        } else if ("toEdit".equals(method)) {
            this.toEdit(req, resp);
        } else if ("delete".equals(method)) {
            this.delete(req, resp);
        } else if ("queryBySname".equals(method)) {
            this.queryBySname(req, resp);
        } else if ("queryBySupply".equals(method)) {
            this.queryBySupply(req, resp);
        } else if ("query".equals(method)) {
            this.query(req, resp);
        } else {
            this.queryAll(req, resp);
        }
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        String pageIndex = req.getParameter("pageIndex");
        String param = req.getParameter("param");
        String type = req.getParameter("type");
        if (pageIndex == null) {
            pageIndex = (String) req.getSession().getAttribute(Constants.PAGE_INDEX);
        }
        if (param == null) {
            param = (String) req.getSession().getAttribute(Constants.PARAM);
        }
        if (type == null) {
            type = (String) req.getSession().getAttribute(Constants.TYPE);
        }
        String sname = null;
        String supply = null;
        if ("sname".equals(type)) {
            sname = param;
        } else if ("supply".equals(type)) {
            supply = param;
        }

        //页面容量
        int pageSize = Constants.pageSize;
        //当前页码
        int currentPageNo = 1;
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.parseInt(pageIndex);
            } catch (NumberFormatException e) {
                resp.sendRedirect("error.jsp");
            }
        }
        //总数量（表）
        int totalCount = supplierService.getCount(sname, supply);

        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);

        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        List<Supplier> supplierList = supplierService.getList(sname, supply, currentPageNo, pageSize);

        req.getSession().setAttribute(Constants.PAGE_INDEX, String.valueOf(currentPageNo));
        req.getSession().setAttribute(Constants.PARAM, param);
        req.getSession().setAttribute(Constants.TYPE, type);

        req.setAttribute("suppliers", supplierList);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("totalCount", totalCount);
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("type", type);
        req.setAttribute("myParam", param);
        req.getRequestDispatcher("/jsp/supplier/list2.jsp").forward(req, resp);
    }

    private void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        List<Supplier> suppliers = supplierService.queryAll();
        req.setAttribute("suppliers", suppliers);
        req.getRequestDispatcher("/jsp/supplier/list.jsp").forward(req, resp);
    }

    private void queryBySname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        String sname = req.getParameter("param");
        List<Supplier> suppliers = supplierService.queryBySname(sname);
        req.setAttribute("suppliers", suppliers);
        req.getRequestDispatcher("/jsp/supplier/list.jsp").forward(req, resp);
    }

    private void queryBySupply(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        String sname = req.getParameter("param");
        List<Supplier> suppliers = supplierService.queryBySupply(sname);
        req.setAttribute("suppliers", suppliers);
        req.getRequestDispatcher("/jsp/supplier/list.jsp").forward(req, resp);
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        Supplier supplier = null;
        String sid = req.getParameter("sid");
        if (sid != null) {
            supplier = supplierService.queryBySid(sid);
            req.setAttribute("supplier", supplier);
        }
        req.getRequestDispatcher("/jsp/supplier/edit.jsp").forward(req, resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        String sid = req.getParameter("sid");
        if (sid != null) {
            if (supplierService.deleteBySid(sid) == 0) {
                req.setAttribute("error", "数据删除失败");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "删除供货商成功");
            }
        }
        req.getRequestDispatcher("/supplier?method=query").forward(req, resp);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SupplierService supplierService = new SupplierService();
        Supplier supplier = new Supplier();
        boolean flag = false;
        String sid = req.getParameter("sid");
        if (!"".equals(sid)) {
            supplier.setSid(Long.valueOf(sid));
            flag = true;
        }
        supplier.setSname(req.getParameter("sname"));
        supplier.setContact(req.getParameter("contact"));
        supplier.setSupply(req.getParameter("supply"));
        supplier.setPhone1(req.getParameter("phone1"));
        supplier.setPhone2(req.getParameter("phone2"));
        supplier.setReputation(req.getParameter("reputation"));
        supplier.setRemark(req.getParameter("remark"));
        if (flag) {
            if (supplierService.updateBySid(supplier) == 0) {
                req.setAttribute("error", "数据更新失败");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "更新供货商成功");
                req.getRequestDispatcher("/supplier?method=query").forward(req, resp);
            }
        } else {
            if (supplierService.insert(supplier) == 0) {
                req.setAttribute("error", "数据插入失败");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "插入供货商成功");
                req.getRequestDispatcher("/supplier?method=query").forward(req, resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
